﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using DiscoveryLogic.DataViz.NetworkDiagram.NetworkData;

namespace DiscoveryLogic.DataViz.NetworkDiagram
{
    /// <summary>
    /// collection of networks
    /// </summary>
    public class WorkspaceManager
    {
        #region network
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static List<string> GetAllNetworkNames()
        {
            List<string> networkNames=new List<string>();
            string appFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            appFolder = Path.Combine(appFolder, "Network Data");
            if (!Directory.Exists(appFolder))
                Directory.CreateDirectory(appFolder);
            DirectoryInfo dirInfo = new DirectoryInfo(appFolder);
            FileInfo[] networkFileInfos = dirInfo.GetFiles("*.net", SearchOption.TopDirectoryOnly);
            if (networkFileInfos != null && networkFileInfos.Length > 0)
            {
                foreach (FileInfo netDataInfo in networkFileInfos)
                {
                    networkNames.Add(Path.GetFileNameWithoutExtension(netDataInfo.FullName));
                }
            }
            return networkNames;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static Dictionary<EntityType, List<Network>> GetAllNetworks()
        {
            Dictionary<EntityType, List<Network>> allNetworks = new Dictionary<EntityType, List<Network>>();
            List<string> allNetworkNames = WorkspaceManager.GetAllNetworkNames();
            foreach(string networkName in allNetworkNames)
            {
                Network network = Network.ReadNetwork(networkName);
                if(allNetworks.ContainsKey(network.EntityType))
                {
                    List<Network> networks = allNetworks[network.EntityType];
                    networks.Add(network);
                    allNetworks[network.EntityType] = networks;
                }
                else
                {
                    List<Network> networks=new List<Network>();
                    networks.Add(network);
                    allNetworks.Add(network.EntityType,networks);
                }
            }
            return allNetworks;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="networkName"></param>
        public static void DeleteNetwork(string networkName)
        {
            string appFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            appFolder = Path.Combine(appFolder, "Network Data");
            if (!Directory.Exists(appFolder))
                Directory.CreateDirectory(appFolder);
            string networkFilePath = Path.Combine(appFolder, networkName + ".net");
            if(File.Exists(networkFilePath))
                File.Delete(networkFilePath);
        }
        #endregion

        #region graph 
        /// <summary>
        /// file path of graph file
        /// </summary>
        /// <returns></returns>
        public static List<string> GetAllGraphFiles()
        {
            List<string> graphNames=new List<string>();
            string appFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            appFolder = Path.Combine(appFolder, "Graph Data");
            if (!Directory.Exists(appFolder))
                Directory.CreateDirectory(appFolder);
            DirectoryInfo dirInfo = new DirectoryInfo(appFolder);
            FileInfo[] graphFileInfos = dirInfo.GetFiles("*.graph", SearchOption.TopDirectoryOnly);
            if (graphFileInfos != null && graphFileInfos.Length > 0)
            {
                foreach (FileInfo graphFile in graphFileInfos)
                {
                    graphNames.Add(graphFile.FullName);
                }
            }
            return graphNames;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="graphName"></param>
        public static void RemoveGraph(string graphName)
        {
            string appFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            appFolder = Path.Combine(appFolder, "Graph Data");
            if (!Directory.Exists(appFolder))
                Directory.CreateDirectory(appFolder);
            string graphFilePath = Path.Combine(appFolder, graphName + ".graph");
            if(File.Exists(graphFilePath))
                File.Delete(graphFilePath);
        }
        #endregion
    }
}
